package com.photographysharing.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.photographysharing.dto.PhotoDTO;
import com.photographysharing.entity.Photos;
import com.photographysharing.exception.ServiceException;
import com.photographysharing.request.SelectByPaging;
import com.photographysharing.response.DataResponse;
import com.photographysharing.response.DatasResponse;
import com.photographysharing.service.PhotosService;
import com.photographysharing.mapper.PhotosMapper;
import org.springframework.stereotype.Service;
import java.util.List;

/**
* @author Z
* @description 针对表【photos】的数据库操作Service实现
* @createDate 2024-05-14 21:15:11
*/
@Service
public class PhotosServiceImpl extends ServiceImpl<PhotosMapper, Photos>
    implements PhotosService{

    private PhotosMapper photosMapper;

    public PhotosServiceImpl(PhotosMapper photosMapper) {
        this.photosMapper = photosMapper;
    }

    public DatasResponse<PhotoDTO> select(SelectByPaging pag){
        int index = pag.getIndex();
        int size = pag.getSize();
        String name = pag.getSearchName();
        int categoryId = Integer.parseInt(pag.getSearchValue1());
        String userName = pag.getSearchValue2();
        String tag = pag.getSearchValue3();
        try {
            List<PhotoDTO>datas = photosMapper.selectByAll((index-1)*size,size,name,userName,tag,categoryId);
            int count = photosMapper.selctCount(name,userName,tag,categoryId);
            return new DatasResponse<>(count,datas);
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }

    }

    @Override
    public PhotoDTO getById(String photoId) {
        try{
            return photosMapper.getById(photoId);
        }catch (Exception e){
            throw new ServiceException("服务异常!");
        }

    }

    @Override
    public DatasResponse<PhotoDTO> selectByUserId(SelectByPaging pag) {
        String userId = pag.getSearchValue1();
        int index = pag.getIndex();
        int size = pag.getSize();
        String name = pag.getSearchName();
        List<PhotoDTO> datas = photosMapper.selectByUserId((index-1)*size,size,name,userId);
        int count = photosMapper.selectCountByUserId(name,userId);
        return new DatasResponse<>(count,datas);
    }

    @Override
    public DatasResponse<PhotoDTO> selectByPag(SelectByPaging pag) {
        int index = pag.getIndex();
        int size = pag.getSize();
        String name = pag.getSearchName();
        int categoryId = Integer.parseInt(pag.getSearchValue1());
        String userName = pag.getSearchValue2();
        String tag = pag.getSearchValue3();
        List<PhotoDTO>datas = photosMapper.selectByPag((index-1)*size,size,name,userName,tag,categoryId);
        int count = photosMapper.selctByPagCount(name,userName,tag,categoryId);
        return new DatasResponse<>(count,datas);
    }

}




